package cn.iocoder.yudao.module.member.controller.admin.message;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.api.message.MessageApi;
import cn.iocoder.yudao.module.system.api.message.MessageTypeApi;
import cn.iocoder.yudao.module.system.api.message.dto.*;
import cn.iocoder.yudao.module.system.controller.admin.messageType.vo.MessageTypeCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.messageType.vo.MessageTypePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.messageType.vo.MessageTypeRespVO;
import cn.iocoder.yudao.module.system.controller.admin.messageType.vo.MessageTypeUpdateReqVO;
import cn.iocoder.yudao.module.system.convert.messageType.MessageTypeConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.messageType.MessageTypeDO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.Arrays;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 -运营端- 站内信类型")
@RestController
@RequestMapping("/member/message/type")
@Validated
public class MemberMessageTypeController {

    private final static List<String> baseMessageType=Arrays.asList("系统通知","实习招聘","实践项目","留言板","其他");

    private final static List<Long> baseMessageTypeId=Arrays.asList(1L,2L,3L,4L,5L);
    @Resource
    private MessageTypeApi messageTypeApi;

    @PostMapping("/create")
    @ApiOperation("创建站内信类型")
    public CommonResult<Long> createMessageType(@Valid @RequestBody MessageTypeCreateReqDTO createReqVO) {
        if(baseMessageType.contains(createReqVO.getName())){
            return error("已存在该类型,不能重复添加");
        }
        return success(messageTypeApi.createMessageType(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新站内信类型")
    public CommonResult<Boolean> updateMessageType(@Valid @RequestBody MessageTypeUpdateReqDTO updateReqVO) {
        if("0".equals(updateReqVO.getStatus()) && baseMessageTypeId.contains(updateReqVO.getId())){
            return error("该类型是基本类型,不允许不发布");
        }
        messageTypeApi.updateMessageType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除站内信类型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:message-type:delete')")
    public CommonResult<Boolean> deleteMessageType(@RequestParam("id") Long id) {
        if(baseMessageTypeId.contains(id)){
            return error("该类型是基本类型,不允许删除");
        }
        messageTypeApi.deleteMessageType(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得站内信类型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
//    @PreAuthorize("@ss.hasPermission('system:message-type:query')")
    public CommonResult<MessageTypeRespDTO> getMessageType(@RequestParam("id") Long id) {
        MessageTypeRespDTO messageTypeRespDTO = messageTypeApi.getMessageType(id);
        return success(messageTypeRespDTO);
    }


    @PostMapping("/page")
    @ApiOperation("获得站内信类型分页")
//    @PreAuthorize("@ss.hasPermission('system:message-type:query')")
    public CommonResult<PageResult<MessageTypeRespDTO>> getMessageTypePage(@RequestBody MessageTypePageReqDTO pageVO) {
        PageResult<MessageTypeRespDTO> pageResult = messageTypeApi.getMessageTypePage(pageVO);
        return success(pageResult);
    }


}
